package api

import (
	"errors"
	"fmt"
	global2 "longmen/server/config/global"
	"longmen/server/pkg/common/helper/const"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	"longmen/server/pkg/protobuf"
	"net/url"
	"strings"

	"github.com/labstack/echo/v4"
	_ "github.com/spf13/cast"
	"golang.org/x/net/context"
	"google.golang.org/grpc/status"
)

// @Summary 支付 获取ISO 内支付 订单号
// @Description 支付 获取ISO 内支付 订单号
// @Accept  json
// @Produce  json
// @Tags Payment
// @Param   sendBody body protobuf.OrderNoReq true "body"
// @Success 200 {object} protobuf.PaymentApply "result"
// @Router /api/payment/isoGetOrderNo [POST]
func (base Controller) IsoGetOrderNo(c echo.Context) error {
	//参数检测
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok && uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.OrderNoReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	req.UserId = int32(uid)
	resp, err := grpc_client.GrpcPaymentClient.IsoGetOrderNo(context.Background(), req)
	global2.Log.Debug("IsoGetOrderNo resp:%+v", resp)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, status.Convert(err).Message(), err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// @Summary ISO产品支付
// @Description ISO产品支付
// @Accept  json
// @Produce  json
// @Tags Payment
// @Param   sendBody body protobuf.ProductPayReq true "body"
// @Success 200 {object} protobuf.ProductPayResp "result"
// @Router /api/payment/isoProductPay [POST]
func (base *Controller) IsoProductPay(c echo.Context) error {
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	global2.Log.Debugf("IsoProductPay 走到这里来")
	if !ok && uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.ProductPayReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	//global.Log.Debug("IsoProductPay:" + c.Get(util.HTTP_KEY_BODY).(string))
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	if len(req.OrderNo) <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("订单号不能为空")))
	}
	if len(req.TransactionId) <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("Apple 转账订单号不能为空")))
	}
	if len(req.TransactionReceipt) <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("Apple 转账作凭证不能为空")))
	}
	req.UserId = int64(uid)
	/*	if len(req.TransactionReceipt)==0{
		req.TransactionReceipt="MIIT3QYJKoZIhvcNAQcCoIITzjCCE8oCAQExCzAJBgUrDgMCGgUAMIIDfgYJKoZIhvcNAQcBoIIDbwSCA2sxggNnMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDwIBAQQDAgEAMAsCARACAQEEAwIBADALAgEZAgEBBAMCAQMwDAIBCgIBAQQEFgI0KzAMAgEOAgEBBAQCAgDlMA0CAQMCAQEEBQwDMzQzMA0CAQ0CAQEEBQIDAiU4MA0CARMCAQEEBQwDMS4wMA4CAQkCAQEEBgIEUDI1NjAYAgEEAgECBBCHl+Z+ejaOyOAl/bjAOT/yMBsCAQACAQEEEwwRUHJvZHVjdGlvblNhbmRib3gwGwIBAgIBAQQTDBFjb20uZHVmaWcubGlleWluZzAcAgEFAgEBBBSD1zy65/nh5JUdUKMeleV9CVFFgTAeAgEMAgEBBBYWFDIwMjItMDgtMDlU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20ncwT8EfhYOFG5k9RzfyqZtAjizUsZAS2L70c5vu0mQPy3lPNNiiPvl4/2vIB+x9OYOLUyDTOMSxv5pPCmv/K/xZpwUJfBdAVhEedNO3iyM7R6PVbyTi69G3cN8PReEnyvFteO3ntRcXqNx+IjXKJdXZD9Zr1KIkIxH3oayPc4FgxhtbCS+SsvhESPBgOJ4V9T0mZyCKM2r3DYLP3uujL/lTaltkwGMzd/c6ByxW69oPIQ7aunMZT7XZNn/Bh1XZp5m5MkL72NVxnn6hUrcbvZNCJBIqxw8dtk2cXmPIS4AXUKqK1drk/NAJBzewdXUhMYIByzCCAccCAQEwgaMwgZYxCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSwwKgYDVQQLDCNBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9uczFEMEIGA1UEAww7QXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkCCA7rV4fnngmNMAkGBSsOAwIaBQAwDQYJKoZIhvcNAQEBBQAEggEAITU8x7WeMpHMnAlCi0yViZfYW0NuntqWDw43NjEzoKwWjGGpZAduIEWHo6kZoala8p5r1KuI7eB614tsPjpKRZhzm+S5DrSW6ZgOe0oUerz+xnOx/Htk5cunHxpFhKqee9GR5WBeU0CgH0pOxXr1x1G/LDCKX0s0cJYLBsgXaKy1piwETWLHcPPZIm1E8luoIJ5f+5dbvaBqWXkS85/hTQ4TPuKlkIrQiqteV1elgsLU2DuH40lstBwqS7cssF6itg8T+jiiszntLwlxah8Jx/MuDIJdhFPZYc997AN6UwkMB/SnqzYai7m/dpgBSY5PPuzfLXcsp24b/pmvfFCQFA=="
	}*/
	setIsoProductDefaultVal(req)
	resp, err := grpc_client.GrpcPaymentClient.IsoProductPay(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

/**
 * @Author admin
 * @Description //设置参数初始值
 * @version: v1.8.0
 * @Date 13:05 2022/8/9
 * @Param
 * @return
 **/
func setIsoProductDefaultVal(req *protobuf.ProductPayReq) {
	payUrlEnv := global2.Config.GetInt("isoProductPay.server.environment")
	if payUrlEnv != _const.PRODUCTION_URL_VAL {
		req.UrlType = _const.SAND_BOX_URL_VAL
	} else {
		req.UrlType = _const.PRODUCTION_URL_VAL
	}
	secretKey := global2.Config.GetString("iso.pay.secret.key")
	if len(secretKey) == 0 {
		req.Password = "eabbace6431a4c1c91bc24299d4eab97"
	} else {
		req.Password = secretKey
	}
	bundleId := global2.Config.GetString("iso.pay.bundleId")
	if len(bundleId) == 0 {
		req.AppleClientId = "com.dufig.lieying"
	} else {
		req.AppleClientId = bundleId
	}

}

// @Summary 克拉充值数量推荐列表
// @Description 克拉充值数量推荐列表
// @Accept  json
// @Produce  json
// @Tags Payment
// @Success 200 {object} protobuf.GoldPricesListResp "result"
// @Router /api/payment/getProductPriceList [POST]
func (base *Controller) GetProductPriceList(c echo.Context) error {
	list, err := grpc_client.GrpcPaymentClient.GetGoldPricesList(context.Background(), &protobuf.Empty{})
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, list, _const.EMPTY_STR)
}

// @Summary 慧收钱
// @Description 慧收钱
// @Accept  json
// @Produce  json
// @Tags Payment
// @Param   sendBody body protobuf.OrderNoReq true "body"
// @Success 200 {object} protobuf.HsqBusinessResp "result"
// @Router /api/payment/applyHsqPayment [POST]
func (base Controller) ApplyHsqPayment(c echo.Context) error {
	return util.BuildFail(c, _const.RPC_FAILED, "敬请期待", nil) //Jan12关闭支付
	//参数检测
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok && uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.OrderNoReq{}

	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	if len(req.PayType) <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请求支付类型为空")))
	}
	//req.PayType != util.HSQ_DYNAMIC_ALL && req.PayType != util.HSQ_DYNAMIC_ALPAY && req.PayType != util.HSQ_DYNAMIC_WECHAT && req.PayType != util.HSQ_DYNAMIC_UNION
	if req.PayType != _const.HSQ_DYNAMIC_ALPAY && req.PayType != _const.HSQ_DYNAMIC_WECHAT && req.PayType != _const.HSQ_NATIVE_WECHAT && req.PayType != _const.HSQ_NATIVE_ALI {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请求支付类型为非法类型")))
	}
	req.UserId = int32(uid)
	//ip, err := util.ExternalIP()
	req.ClientIp = "127.0.0.1"
	//req.ClientIp = cast.ToString(ip)
	global2.Log.Debug("ip" + req.ClientIp)
	resp, err := grpc_client.GrpcPaymentClient.ApplyHsqPayment(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, status.Convert(err).Message(), err)
	}

	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// mpfd	multipart/form-data
// x-www-form-urlencoded	application/x-www-form-urlencoded
// @Summary 慧收钱回调
// @Description 慧收钱回调
// @Accept  application/x-www-form-urlencoded
// @Produce  application/x-www-form-urlencoded
// @Tags Payment
// @Param method formData string true "CALLBACK"
// @Param version formData string true "接口版本：1.0"
// @Param format formData string true "业务请求参数格式，支持：JSON、XML"
// @Param merchantNo formData string true "商户在慧收钱的商户号，由慧收钱生成并下发"
// @Param signType formData string true "RSA2:签名机制查看说明"
// @Param signContent formData string true "业务请求参数"
// @Param sign formData string  false "签名数据"
// @Success 200 {object} protobuf.HsqBusinessResp "result"
// @Router /api/payment/hsqPaymentCallBack [POST]
func (base Controller) HsqPaymentCallBack(c echo.Context) error {
	body := c.Get("body").(string)
	//global.Log.Info(body)
	global2.Log.Debug("HsqPaymentCallBack=>" + body)
	rpcReq := getParam(body)
	rpcReq.SignContent, _ = url.QueryUnescape(rpcReq.SignContent)
	resp, err := grpc_client.GrpcPaymentClient.HsqPaymentCallBack(context.Background(), rpcReq)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, status.Convert(err).Message(), err)
	}
	if resp.Code == _const.RPC_SUCCEED {
		return util.BuildPaySuccess(c)
	} else {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New("fail"))
	}
}

func getParam(body string) *protobuf.HsqBasic {
	rpcReq := &protobuf.HsqBasic{}
	split := strings.Split(body, "&")
	for _, val := range split {
		arr := strings.Split(val, "=")
		switch arr[0] {
		case "method":
			rpcReq.Method = arr[1]
		case "version":
			rpcReq.Version = arr[1]
		case "format":
			rpcReq.Format = arr[1]
		case "merchantNo":
			rpcReq.MerchantNo = arr[1]
		case "signType":
			rpcReq.SignType = arr[1]
		case "signContent":
			rpcReq.SignContent = arr[1]
		case "sign":
			rpcReq.Sign = arr[1]
		}
	}
	return rpcReq
}

// @Summary 支付宝
// @Description 支付宝
// @Accept  json
// @Produce  json
// @Tags Payment
// @Param   sendBody body protobuf.OrderNoReq true "body"
// @Success 200 {object} protobuf.HsqBusinessResp "result"
// @Router /api/payment/applyAliPayment [POST]
func (base Controller) ApplyAliPayment(c echo.Context) error {
	//参数检测
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok && uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.OrderNoReq{}

	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	req.UserId = int32(uid)
	resp, err := grpc_client.GrpcPaymentClient.ApplyAliPayment(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, status.Convert(err).Message(), err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// @Summary 慧收钱
// @Description 慧收钱
// @Accept  json
// @Produce  json
// @Tags Payment
// @Param   sendBody body protobuf.AliPayCallBackReq true "body"
// @Success 200 {object} protobuf.HsqBusinessResp "result"
// @Router /api/payment/aliPaymentCallBack [POST]
func (base Controller) AliPaymentCallBack(c echo.Context) error {
	req := &protobuf.AliPayCallBackReq{}
	body := c.Get("body").(string)
	//global.Log.Info(body)
	global2.Log.Debug(body)
	req.Params = body
	resp, err := grpc_client.GrpcPaymentClient.AliPaymentCallBack(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, status.Convert(err).Message(), err)
	}
	if resp.Code == _const.RPC_SUCCEED {
		return util.BuildPaySuccess(c)
	} else {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New("fail"))
	}
}

// @Summary 充值记录查询
// @Description 充值记录查询
// @Accept  json
// @Produce  json
// @Tags Payment
// @Param   sendBody body protobuf.PaymentListReq true "body"
// @Success 200 {object} protobuf.PaymentListResp "result"
// @Router /api/payment/getPaymentList [POST]
func (base Controller) GetPaymentList(c echo.Context) error {
	//参数检测
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok && uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}
	req := &protobuf.PaymentListReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	req.UserId = int32(uid)
	req.Page = int32(string2.GetDefaultNum(int(req.Page), _const.LIST_PAGE_DEFAULT))
	req.Size = int32(string2.GetDefaultNum(int(req.Size), _const.LIST_SIZE_DEFAULT))
	resp, err := grpc_client.GrpcPaymentClient.GetPaymentList(context.Background(), req)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, status.Convert(err).Message(), err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}
